﻿using Order.Domain;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace Order.Common
{
    public static class AggregateRootRepository
    {
        static ConcurrentDictionary<string, IAggregationRoot> _dictionary = new ConcurrentDictionary<string, IAggregationRoot>();

        public static async void AddAsync(IAggregationRoot aggregation)
        {
            if (!_dictionary.ContainsKey(aggregation.Id.ToString()))
            {
                _dictionary.GetOrAdd(aggregation.Id.ToString(), aggregation);
            }
            await Task.CompletedTask;
        }

        public static async Task<T> GetAsync<T>(string aggregationRootId) where T : class, IAggregationRoot, new()
        {
            IAggregationRoot aggregation;
            if (_dictionary.ContainsKey(aggregationRootId))
            {
                _dictionary.TryGetValue(aggregationRootId, out aggregation);
                await Task.CompletedTask;
                return aggregation as T;
            }
            await Task.CompletedTask;
            return null;

        }
    }
}
